﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Pages_UserControls_ShowListUser : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String ten = Session["Username"].ToString();

            HienthiListHoso(ten);
        }

    }
    void HienthiListHoso(String Ten)
    {

        UsersBLL userBLL = new UsersBLL();
       // Users admin = userBLL.UserDAL.GetUserByUsername(Ten);
        List<UserProfiles> listp = userBLL.GetIUserProfiles.FindUserWithByName(Ten);


        CollectionPager1.MaxPages = 10;

        CollectionPager1.PageSize = 3; // số items hiển thị trên một trang

        CollectionPager1.DataSource = listp;

        CollectionPager1.BindToControl = DataList1;

        DataList1.DataSource = CollectionPager1.DataSourcePaged;
        DataList1.DataBind();
        //

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Ketban")
        {
            UsersBLL userBLL = new UsersBLL();
           // List<UserProfiles> listp = userBLL.UserProfileDAL.FindUserWithByName(Ten);
            string username = ((Users)Session["User"]).Username.ToString();
            Users user = userBLL.GetIUsers.GetUserByUsername(username);

         //   DataListItem dong = e.Item as DataListItem;
          //  String name = DataBinder.Eval(dong.DataItem, e.CommandArgument.ToString(), "{0}");
            ((Button)e.Item.FindControl("btnKetban")).CommandArgument = string.Format("{0}", e.CommandArgument.ToString());


            String name = e.CommandArgument.ToString();
         //   ((Button)e.Item.FindControl("btnKetban")).CommandArgument = name;

            Users myfriend = userBLL.GetIUsers.GetUserByUsername(name);
            
            ////Users myfriend = userBLL.UserDAL.GetUserByUsername(((Button)e.Item.FindControl("btnKetban")).Text);

            try
            {

                bool kq = userBLL.GetIFriends.InsertFriend(user, myfriend);
                if (kq == true)
                {
                    Response.Write("<script>alert('thanh cong')</script>");
                }
                else
                {
                    Response.Write("<script>alert('thanh cong')</script>");

                }
            }
            catch
            {
                Response.Write("<script>alert('User  này đã là bạn của bạn rồi')</script>");
            }
         //   Response.Write(name);
            
          
            HienthiListHoso(Session["Username"].ToString());

            

        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType ==  ListItemType.Item)
        {
            
           DataListItem  dong = e.Item as DataListItem;
           String name = DataBinder.Eval(dong.DataItem, "user.Username", "{0}");
            ((Button)e.Item.FindControl("btnKetban")).CommandArgument = string.Format("{0}",name);
            //btnKetban
         
        }

    }
}